<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="../plugins/select2/select2.css" />
	<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
	<script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
	<!--bootstrap-fileinput-->

	<link rel="stylesheet" type="text/css" href="/js/bootstrap-fileinput/css/fileinput.css">
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/fileinput.js"></script>
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/locales/zh.js"></script>

	<script>
		$(function () {
			show();
			fileInit();
			fileInit1();
			$("#category").select2();
			$("#categorys").select2();
		})

		function show() {
			$.ajax({
				url:"/content/listContent",
				type:"post",
				dataType:"json",
				success:function (result) {
					var data=result.data;
					var tbody ='';
					for (var i=0;i<data.length;i++){
						tbody += '<tr>';
						tbody += '<td><input  type="checkbox" name="box" value="'+data[i].id+'"></td>';
						tbody += '<td>'+data[i].id+'</td>';
						tbody += '<td>'+data[i].categoryId+'</td>';
						tbody += '<td>'+data[i].title+'</td>';
						tbody += '<td>'+data[i].url+'</td>';
						tbody += '<td><img src="'+data[i].pic+'" width="120px" height="90px"></td>';
						tbody += '<td>'+data[i].sortOrder+'</td>';
						if (data[i].status ==1){
							tbody += '<td>有效</td>';
						}else {
							tbody += '<td>无效</td>';
						}
						tbody += '<td class="text-center">';
						tbody += '<button type="button" class="btn bg-olive btn-xs" onclick="getContent('+data[i].id+')" data-toggle="modal" data-target="#updateditModal">修改</button>';
						tbody += '</td>';
						tbody += '</tr>';
					}
					$("#tbody").html(tbody);
				},
				error:function () {
					alert("展示失败");
				}
			})
		}

		//增加图片
		function fileInit(){
			$('#uimg').fileinput({
				language: 'zh',
				uploadUrl: '/content/upLoadFile',
				maxFileCount :3, // 最多同时传送几个图片，需要加multiple注解
				enctype : 'multipart/form-data',
				maxFileSize : 1024, //单位为kb
				allowedFileExtensions : ['jpg', 'gif', 'png'],
				browseClass: 'btn btn-primary'
			});

			$('#uimg').on('fileuploaded',function(event, data, previewId, index){
				if(data.response){//如果有返回值 那么就最终结果就是true
					//   var imgUrl = data.response.imgUrl;//原先上传图片
					var imgUrl = data.response.data;//阿里云上传 setData(url)在后段方法里把url设置给了data，所以想拿到url就得.data
					$("#pic").val(imgUrl);
				}
			})
		}

		//增加
		function insert() {
			//广告分类
			var categoryData = $("#category").select2("data");
			//获取广告id
			var categoryId=categoryData.id;
			var title = $("#title").val();
			var url = $("#url").val();
			var pic = $("#pic").val();
			var status="";
			if($("#status").is(":checked")){
				status="1"
			}else {
				status="0"
			}
			var sortOrder = $("#sortOrder").val();
			$.ajax({
				url:"/content/insertContent",
				type:"post",
				dataType:"json",
				data:{categoryId:categoryId,title:title,url:url,pic:pic,status:status,sortOrder:sortOrder},
				success:function (result) {
					if (result.code ==200){
						console.log(result.data);
						alert("添加成功")
						location.reload();
					}else {
						console.log(result)
						alert("添加失败")
					}
				},
				error:function () {
					alert("系统错误1");
				}
			})
		}

		//回显
		function getContent(id) {
			$.ajax({
				url:"/content/getContent",
				type:"post",
				dataType:"json",
				data:{id:id},
				success:function (result) {
					if (result.code ==200){
						var data=result.data;
						$("#ids").val(data.id);
						$("#categorys").val(data.categoryId).trigger("change");
						$("#titles").val(data.title);
						$("#urls").val(data.url);
						$("#sortOrders").val(data.sortOrder);
						var tbody="";//图片回显
						tbody +="<td><img src='"+data.pic+"' width='200px' height='150px'><input type='hidden' value='"+data.pic+"' id='imgg'></td>";
						$("#img").html(tbody);
						if (data.status ==1){
							$("#statuss").attr("checked",true);
						}else {
							$("#statuss").attr("checked",false);
						}
					}
				},
				error:function () {
					alert("系统错误2");
				}
			})
		}

		//修改图片
		function fileInit1(){
			$('#uimgs').fileinput({
				language: 'zh',
				uploadUrl: '/content/upLoadFile',
				maxFileCount :3, // 最多同时传送几个图片，需要加multiple注解
				enctype : 'multipart/form-data',
				maxFileSize : 1024, //单位为kb
				allowedFileExtensions : ['jpg', 'gif', 'png'],
				browseClass: 'btn btn-primary'
			});
			$('#uimgs').on('fileuploaded',function(event, data, previewId, index){
				if(data.response){//如果有返回值 那么就最终结果就是true
					//   var imgUrl = data.response.imgUrl;//原先上传图片
					var imgUrl = data.response.data;//阿里云上传 setData(url)在后段方法里把url设置给了data，所以想拿到url就得.data
					$("#pics").val(imgUrl);
				}
			})
		}

		//修改
		function updateContent() {
			var id=$("#ids").val();
			//广告分类 select2
			var select2 = $("#categorys").select2("data");
			//获取categoryId
			var categoryId = select2.id;
			var title=$("#titles").val();
			var url=$("#urls").val();
			var sortOrder= $("#sortOrders").val();
			var pic="";
			if ($("#pics").val() != null && $("#pics").val() != ''){
				alert(1111)//没有图片新添加
				pic =$("#pics").val();
			}else {
				alert(2222)//原有的图片
				pic=$("#imgg").val();
			}
			var status = "";
			if($("#statuss").is(":checked")){
				status +="1"
			}else {
				status +="0"
			}
			$.ajax({
				url:"/content/updateContent",
				type:"post",
				dataType:"json",
				data:{id:id,categoryId:categoryId,title:title,url:url,sortOrder:sortOrder,pic:pic,status:status},
				success:function (result) {
					alert("修改成功");
					window.location.reload();
				},
				error:function () {
					alert("系统错误")
				}
			})
		}

		//删除
		function deleteContent() {
			var boxs=$("[type=checkbox]:checked");
			var ids=[];
			$(boxs).each(function (i,e) {
				ids.push(e.value);
			})
			if (ids.length<=0){
				return;
			}

			$.ajax({
				url:"/content/deleteContent",
				type:"post",
				dataType:"json",
				data:{ids:ids},
				success:function (result) {
					alert("删除成功");
					window.location.reload();
				},
				error:function () {
					alert("系统错误3")
				}
			})
		}
	</script>
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->

                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteContent()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">

                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th>
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>
									      <th class="sorting">图片</th>
									      <th class="sorting">排序</th>
									      <th class="sorting">是否有效</th>
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">
			                          <tr>
			                              <td><input  type="checkbox"></td>
				                          <td>1</td>
									      <td>1</td>
									      <td>促销海报1</td>
									      <td>http://wwww.hb.com/hd1.html</td>
									      <td>
									      	<img alt="" src="" width="100px" height="50px">
									      </td>
									      <td>1</td>
									      <td>有效</td>
		                                  <td class="text-center">
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>
		                                  </td>
			                          </tr>
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->


<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">

			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
						<select class="form-control" id="category" style="width: 100%">
							<option th:each=" cg: ${category}" th:value="${cg.id}">[[${cg.name}]]</option>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" placeholder="标题" id="title">  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" placeholder="URL" id="url" >  </td>
		      	</tr>
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" placeholder="排序" id="sortOrder">  </td>
		      	</tr>
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
									<label for="uimg">上传图片</label>
									<input type="file"class="form-control" name="myFile" id="uimg">
									<input type="text" name="pic" id="pic">
							<!--	<td>
									<img  src="{{entity.pic}}" width="200px" height="100px">
								</td>-->
							</tr>
						</table>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input type="checkbox" class="icheckbox_square-blue"  id="status">
		      		</td>
		      	</tr>
			 </table>

		</div>
		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insert()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>


  <!-- 修改窗口 -->
  <div class="modal fade" id="updateditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel">广告编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <input type="text" id="ids">
					  </tr>
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <select class="form-control" id="categorys" style="width: 100%">
								  <option th:each=" cg: ${category}" th:value="${cg.id}">[[${cg.name}]]</option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td><input  class="form-control" placeholder="标题" id="titles">  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input  class="form-control" placeholder="URL" id="urls" >  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input  class="form-control" placeholder="排序" id="sortOrders">  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>
							  <table>
								  <tr>
									  <td>
										  <label for="uimg">上传图片</label>
										  <input type="file" class="form-control" name="myFile" id="uimgs">
										  <input type="text" name="pic" id="pics">
										  <!--	<td>
                                                  <img  src="{{entity.pic}}" width="200px" height="100px">
                                              </td>-->
									  <td id="img"></td>
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input type="checkbox" class="icheckbox_square-blue"  id="statuss">
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateContent()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>

</html>
